import hilog from '@ohos.hilog';
import utils from '../core/utils';
import  { Level,Logger, LoggerFactory }  from '@ohos/logback';

export class logger {
  private readonly log: Logger;
  private readonly domain: number;
  private readonly prefix: string;
  private readonly format: string = '%{public}s, %{public}s';

  constructor() {
    this.domain = 0xFF00;
    this.prefix = 'bilibili_manga';
    this.log = LoggerFactory.getLogger("com.michael.bilibili_manga_log");
    this.log.setLevel(Level.INFO);
  }

  public info(msg: string, show?: boolean) {
    hilog.info(this.domain, this.prefix, this.format, msg);
    this.log.info(msg);
    if (show)
      utils.showPopup('发生错误：' + msg);
  }

  public error(e: any, show?: boolean) {
    var msg = `error:${JSON.stringify(e)}`;
    hilog.error(this.domain, this.prefix, this.format, msg);
    this.log.error(msg);
    if (show)
      utils.showPopup('发生错误：' + msg);
  }
}

export default new logger()